package spring4.controller;

import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import spring4.model.User;

/**
 *
 * @author kent
 */
@Controller
public class DefaultController {

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public String createUser(@Valid @ModelAttribute User user, BindingResult result, Model model) {
        if (result.hasErrors()) {
            model.addAttribute("errors", result.getFieldErrors());
        } else {
            model.addAttribute("msg", "user is " + user.toString());
        }
        return "index";
    }
}
